package com.ruoyi.manage.controller;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.domain.DeviceInfo;
import com.ruoyi.manage.domain.DeviceNetworkCard;
import com.ruoyi.manage.domain.DeviceHdd;
import com.ruoyi.manage.service.IDeviceInfoService;
import com.ruoyi.manage.service.IDeviceNetworkCardService;
import com.ruoyi.manage.service.IDeviceHddService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-22
 */
@RestController
@RequestMapping("/manage/deviceManage")
public class DeviceInfoController extends BaseController
{
    @Autowired
    private IDeviceInfoService deviceInfoService;
    
    @Autowired
    private IDeviceNetworkCardService deviceNetworkCardService;
    
    @Autowired
    private IDeviceHddService deviceHddService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceInfo deviceInfo)
    {
        startPage();
        List<DeviceInfo> list = deviceInfoService.selectDeviceInfoList(deviceInfo);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceInfo deviceInfo)
    {
        List<DeviceInfo> list = deviceInfoService.selectDeviceInfoList(deviceInfo);
        ExcelUtil<DeviceInfo> util = new ExcelUtil<DeviceInfo>(DeviceInfo.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return success(deviceInfoService.selectDeviceInfoByDeviceId(deviceId));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceInfo deviceInfo)
    {
        int result = deviceInfoService.insertDeviceInfo(deviceInfo);
        if (result > 0) {
            // 返回包含设备编码的完整设备信息
            return success(deviceInfo);
        }
        return error("新增设备失败");
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceInfo deviceInfo)
    {
        return toAjax(deviceInfoService.updateDeviceInfo(deviceInfo));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(deviceInfoService.deleteDeviceInfoByDeviceIds(deviceIds));
    }
    
    /**
     * 获取设备网卡信息
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:query')")
    @GetMapping(value = "/getDeviceNetworkCardInfo/{deviceCode}")
    public AjaxResult getDeviceNetworkCardInfo(@PathVariable("deviceCode") String deviceCode)
    {
        List<DeviceNetworkCard> networkCardList = deviceNetworkCardService.selectDeviceNetworkCardByDeviceCode(deviceCode);
        return success(networkCardList);
    }
    
    /**
     * 保存设备网卡信息
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:edit')")
    @Log(title = "设备网卡信息", businessType = BusinessType.UPDATE)
    @PostMapping("/saveDeviceNetworkCardInfo")
    public AjaxResult saveDeviceNetworkCardInfo(@RequestBody Map<String, Object> requestBody)
    {
        String deviceCode = (String) requestBody.get("deviceCode");
        List<Map<String, Object>> networkCardInfoList = (List<Map<String, Object>>) requestBody.get("networkCardInfoList");
        
        if (deviceCode != null && networkCardInfoList != null && !networkCardInfoList.isEmpty())
        {
            // 转换为DeviceNetworkCard对象列表
            List<DeviceNetworkCard> networkCards = new ArrayList<>();
            for (Map<String, Object> cardMap : networkCardInfoList)
            {
                DeviceNetworkCard card = new DeviceNetworkCard();
                card.setDeviceCode(deviceCode);
                if (cardMap.containsKey("cardId")) {
                    card.setCardId(cardMap.get("cardId") != null ? Long.valueOf(cardMap.get("cardId").toString()) : null);
                }
                card.setIpAddress(cardMap.get("ipAddress") != null ? (String) cardMap.get("ipAddress") : null);
                card.setMacAddress(cardMap.get("macAddress") != null ? (String) cardMap.get("macAddress") : null);
                card.setRemark(cardMap.get("remark") != null ? (String) cardMap.get("remark") : null);
                networkCards.add(card);
            }
            
            // 保存网卡信息列表
            deviceNetworkCardService.saveDeviceNetworkCardList(deviceCode, networkCards);
        }
        return success();
    }
    
    /**
     * 获取设备硬盘信息
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:query')")
    @GetMapping(value = "/getDeviceHddInfo/{deviceCode}")
    public AjaxResult getDeviceHddInfo(@PathVariable("deviceCode") String deviceCode)
    {
        List<DeviceHdd> hddList = deviceHddService.selectDeviceHddByDeviceCode(deviceCode);
        return success(hddList);
    }
    
    /**
     * 保存设备硬盘信息
     */
    @PreAuthorize("@ss.hasPermi('manage:deviceManage:edit')")
    @Log(title = "设备硬盘信息", businessType = BusinessType.UPDATE)
    @PostMapping("/saveDeviceHddInfo")
    public AjaxResult saveDeviceHddInfo(@RequestBody Map<String, Object> requestBody)
    {
        String deviceCode = (String) requestBody.get("deviceCode");
        List<Map<String, Object>> hddInfoList = (List<Map<String, Object>>) requestBody.get("hddInfoList");
        
        if (deviceCode != null && hddInfoList != null && !hddInfoList.isEmpty())
        {
            // 转换为DeviceHdd对象列表
            List<DeviceHdd> hdds = new ArrayList<>();
            for (Map<String, Object> hddMap : hddInfoList)
            {
                DeviceHdd hdd = new DeviceHdd();
                hdd.setDeviceCode(deviceCode);
                if (hddMap.containsKey("hddId")) {
                    hdd.setHddId(hddMap.get("hddId") != null ? Long.valueOf(hddMap.get("hddId").toString()) : null);
                }
                hdd.setHddType(hddMap.get("hddType") != null ? (String) hddMap.get("hddType") : null);
                hdd.setHddSerial(hddMap.get("hddSerial") != null ? (String) hddMap.get("hddSerial") : null);
                if (hddMap.containsKey("hddCapacity")) {
                    Object capacityObj = hddMap.get("hddCapacity");
                    if (capacityObj != null) {
                        // 处理可能的数字类型转换问题
                        if (capacityObj instanceof Number) {
                            hdd.setHddCapacity(new BigDecimal(capacityObj.toString()));
                        } else {
                            try {
                                hdd.setHddCapacity(new BigDecimal(capacityObj.toString()));
                            } catch (Exception e) {
                                // 转换失败时使用null或默认值
                                hdd.setHddCapacity(null);
                            }
                        }
                    }
                }
                hdd.setHddStatus(hddMap.get("hddStatus") != null ? (String) hddMap.get("hddStatus") : null);
                hdd.setRemark(hddMap.get("remark") != null ? (String) hddMap.get("remark") : null);
                hdds.add(hdd);
            }
            
            // 保存硬盘信息列表
            deviceHddService.saveDeviceHddList(deviceCode, hdds);
        }
        return success();
    }
}
